package village.rainbow.slipin.uicontrol;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;

public class BaseArrayAdapter extends ArrayAdapter<Object>
{
	protected Context mContext;
	protected int mResID = 0;
	protected int mItemHeight = 0;
	protected ResizeLayout mResizeLayout = null;
	
	@SuppressWarnings("unchecked")
	public BaseArrayAdapter(Context context, int resID, Object items, ResizeLayout resizeLayout)
	{
		super(context, resID, (List<Object>)items);

		mContext = context;
		mResID = resID;
		mResizeLayout = resizeLayout;
	}
	public BaseArrayAdapter(Context context, int resID, Object items, ResizeLayout resizeLayout, int itemHeight)
	{
		super(context, resID, (List<Object>)items);

		mContext = context;
		mResID = resID;
		mResizeLayout = resizeLayout;
		if (mResizeLayout != null)
			mItemHeight = (int)(itemHeight * mResizeLayout.getChildRatioH());
		else
			mItemHeight = itemHeight;
	}
	
	public void setResizeLayout(ResizeLayout resizeLayout){
		mResizeLayout = resizeLayout;
		notifyDataSetChanged();
	}
	
	protected void setItemHeight(int itemHeight)
	{
		if (mResizeLayout == null)
			return;
		
		mItemHeight = (int)(itemHeight * mResizeLayout.getChildRatioH());
	}
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		
		if (v != null)
			return v;
		
		// 뷰가 null 일때 View 생성
		LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = vi.inflate(mResID, null);
		if (v.getLayoutParams() == null)
			v.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight));
		v.getLayoutParams().height = mItemHeight;
		if (mResizeLayout != null)
			mResizeLayout.resizeChildSize((ViewGroup)v, parent.getWidth(), mItemHeight);
		
		return  v;
	}	
}